package com.hula.onion.book.server.spider.platforms.jkd;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.SpiderCommon;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.spider.platforms.erong.ERongSpiderHelper;
import com.hula.onion.book.server.utils.CookieUtils;
import com.hula.onion.book.server.utils.DateUtils;
import com.hula.onion.book.server.utils.HttpUtils;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:金开贷
 * Create Date:2018/1/30 19:33
 *
 * @author liangbing
 * @version 1.0
 */
@Component("jkdSpider")
public class JKDSpider extends SpiderCommon {

    /**
     * 登录页面URL
     */
    private static final String LOGIN_PAGE_URL = "http://m.jkd.com/login.html";

    /**
     * 登录URL
     */
    private static final String LOGIN_URL = "https://www.jkd.com/login";

    /**
     * 验证码URL
     */
    private static final String CAPTCHA_URL = "https://www.jkd.com/getValidateCode";

    /**
     * 获取账户信息URL
     */
    private static final String GET_ACCOUNT_INFO_URL = "https://www.myerong.com/userInfo.action";

    @Autowired
    private JKDSpiderHelper jkdSpiderHelper;

    /**
     * 执行爬取操作
     * 1.获取 RSA key
     * 2.组装模拟登陆参数，模拟登陆
     * 3.爬取用户详细信息
     */
    @Override
    public CommonResponse execute() {

        CommonResponse commonResponse = new CommonResponse(ResponseStatusEnum.FAIL);

        HttpClientContext httpContext = HttpClientContext.create();
        BasicCookieStore cookieStore = new BasicCookieStore();
        httpContext.setCookieStore(cookieStore);

        Map<String, Object> paraMap = jkdSpiderHelper.installRequestParams(loginForm);

        //1. 从缓存中获取cookie，设置到cookie store中
        CookieUtils.setCookie(loginForm.getPlatformId(), loginForm.getUserId(), cookieStore);

        //模拟登陆
        String loginStr = HttpUtils.post(LOGIN_URL, paraMap, httpContext);

        logger.info("[JKDSpider] Http response result:{}", loginStr);

        if (!StringUtils.isEmpty(loginStr)) {

            String status = JSONObject.parseObject(loginStr).getString("status");

            //登陆成功
            if ("200".equals(status)) {

                // 爬取数据
                String html = HttpUtils.get(GET_ACCOUNT_INFO_URL, httpContext);

                AccountInfo accountInfo = jkdSpiderHelper.parseAccountBalance(html);

                commonResponse = new CommonResponse(ResponseStatusEnum.SUCCESS);
                commonResponse.setData(accountInfo);

                return commonResponse;

            } else {
                commonResponse = new CommonResponse(ResponseStatusEnum.ACCOUNT_ERROR);

                commonResponse.setMsg(JSONObject.parseObject(loginStr).getString("desc"));

                return commonResponse;
            }
        }

        return commonResponse;
    }

    @Override
    public String getCaptchaURL() {
        return CAPTCHA_URL;
    }

    @Override
    public String getLoginPageURL() {
        return LOGIN_PAGE_URL;
    }

}

